import Toast from "../../../miniprogram_npm/@vant/weapp/toast/toast";
import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog";
import Big from "../../../utils/big.min";
import {
  ajaxGetProductDetails,
  ajaxGetSetMealDetails
} from '../../../api/ordering'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //自定义导航高度
    customNavHeight: app.globalData.customNavInfo.navHeight,
    baseURL: app.globalData.config.baseURL,
    //商品详情数据
    pageData: null,
    //购买数据
    purchaseData: {
      totalMoney: 0,
      totalNum: 1
    },
    //上页面参数
    params: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    let {
      id,
      type,
      num,
      backNum
    } = options;
    this.setData({
      params: options || {}
    })
    //loading
    Toast.loading({
      message: '正在加载',
      forbidClick: true,
      duration: 0
    });
    try {
      let {
        data
      } = await (type == 1 ? ajaxGetProductDetails({
        productId: id
      }) : ajaxGetSetMealDetails({
        packageId: id
      }));
      data = data || {};
      let productList = [],
        pageData = Object.assign({}, data, this.getProductStructure(data))
      if (type == 1) {
        productList.push(this.getProductStructure(data))
      } else {
        (pageData.detailOutVo || []).forEach(item => {
          productList.push(this.getProductStructure(item))
        })
      }
      pageData.productList = productList;
      this.setData({
        pageData: pageData,
        purchaseData: {
          totalMoney: parseFloat(pageData.productPrice).toFixed(2),
          totalNum: 1
        }
      })
      await app.myNextTick()
      Toast.clear();
    } catch (err) {
      await app.myNextTick()
      Toast.clear();
      Toast({
        type: 'fail',
        forbidClick: true,
        message: err.msg || '错误',
        onClose: () => {
          wx.navigateBack({
            delta: 1
          })
        },
      });
    }
  },
  // 获取产品结构
  getProductStructure(data) {
    return {
      productName: data.productName || data.packageName,
      productImage: data.productImage || data.packageImage,
      productPrice: data.productPrice || data.sumAmount || 0,
    }
  },
  //预览图片
  previewImage() {
    wx.previewImage({
      current: this.data.pageData.productImage,
      urls: [this.data.pageData.productImage]
    })
  },
  // 更新商品
  updateCommodity(e) {
    let {
      type
    } = e.currentTarget.dataset, {
      pageData,
      purchaseData
    } = this.data;
    //当前数量
    let currentNum = purchaseData.totalNum;
    currentNum += type == 'add' ? 1 : -1;
    if (currentNum <= 0) {
      return;
    }
    this.setData({
      purchaseData: {
        totalMoney: Big.myToFixed(Big.multiply(currentNum, pageData.productPrice), 2),
        totalNum: currentNum
      }
    })
  },
  // 加入购物车
  async submit() {
    let {
      purchaseData,
      pageData,
      params
    } = this.data;

    // 寻找点单页面调用更新方法
    let page = app.getQueryUrlPage('pages/ordering/index');
    if (page && page.updateDetailsPageProduct) {
      page.updateDetailsPageProduct({
        packageId: pageData.packageId,
        productId: pageData.productId,
        productPrice: pageData.productPrice,
        num: purchaseData.totalNum
      });
    }

    // 返回点单页面
    wx.navigateBack({
      delta: parseInt(params.backNum || 1)
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  }
})